const Memcached = require('memcached')
const memConf = global.sysConfig.memcached;
const mem = new Memcached(memConf.host)

exports.set = function(key, value, callback) {
	if (!key)
		return false;
	if (typeof value == 'undefined')
		value = '';
	let lt = 10000
	if (typeof callback == 'function') {
		mem.set(key, value, lt, callback)
		return
	}
	return new Promise((resolve, reject) => {
		mem.set(key, value, lt, resolve)
	})
}

exports.get = function(key, callback) {
	if (!key)
		return false;
	if (typeof callback == 'function') {
		mem.get(key, callback)
		return
	}
	return new Promise((resolve, reject) => {
		mem.get(key, function(err, data) {
			if (err)
				return reject(err)
			return resolve(data)
		})
	})
}

exports.del = function(key, callback) {
	if (!key)
		return false;
	if (typeof callback == 'function') {
		mem.del(key, callback)
		return
	}
	return new Promise((resolve, reject) => {
		mem.del(key, function(err, data) {
			if (err)
				return reject(err)
			return resolve(data)
		})
	})
}